package com.xdtech.play.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.play.dao.PlayStakeDao;
import com.xdtech.play.model.PlayStake;
import com.xdtech.play.service.PlayStakeService;
import com.xdtech.play.vo.PlayStakeItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2016-05-08 23:48:33
 * @since 1.0
 * @see
 */
@Service
public class PlayStakeServiceImpl implements PlayStakeService {
	private Log log = LogFactory.getLog(PlayStakeServiceImpl.class);
	@Autowired
	private PlayStakeDao playStakeDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param entity
	 */
	public void save(PlayStake entity) {
		playStakeDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<PlayStake> entities) {
		playStakeDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param entity
	 */
	public void delete(PlayStake entity) {
		playStakeDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		playStakeDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param id
	 * @return
	 */
	public PlayStake get(Long id) {
		return playStakeDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @return
	 */
	public List<PlayStake> getAll() {
		return playStakeDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> playStakes = null;
		long rows = 0;
		if (pg!=null) {
			Page<PlayStake> page = playStakeDao.findPage(pg,"from PlayStake order by createTime desc", values);
			playStakes = BeanUtils.copyListProperties(
					PlayStakeItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<PlayStake> playStakeList = playStakeDao.find("from PlayStake order by id desc", values);
			playStakes = BeanUtils.copyListProperties(
					PlayStakeItem.class, playStakeList);
			rows = playStakes.size();
		}
		results.put("total", rows);
		results.put("rows", playStakes);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdatePlayStake(PlayStakeItem item) {
		PlayStake playStake = null;
		if (item.getId()==null) {
			playStake = new PlayStake();
		}else {
			playStake = playStakeDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(playStake, item);
		playStakeDao.save(playStake);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param playStakeId
	 * @return
	 */
	public PlayStakeItem loadPlayStakeItem(Long playStakeId) {
		PlayStake playStake = playStakeDao.get(playStakeId);
		return PlayStakeItem.createItem(playStake);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deletePlayStakeInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deletePlayStakeInfo(List<Long> playStakeIds) {
		for (Long id : playStakeIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:48:33
	 * @modified by
	 * @return
	 */
	@Override
	public List<PlayStakeItem> loadItems() {
		List<PlayStake> playStakes = getAll();
		return PlayStakeItem.createItems(playStakes);
	}

}
